package com.lg.partition12;

import java.io.*;
import java.util.Arrays;
import java.util.PriorityQueue;

/**
 * @author `RKC`
 * @date 2022/3/23 9:13
 */
public class P1119灾后重建_dijkstra {

    private static final int N = 210, M = 40010, INF = 0x3f3f3f3f;
    private static int[] h = new int[N], hw = new int[N], e = new int[M], ne = new int[M], w = new int[M];
    private static int[] dist = new int[N];
    private static boolean[] st = new boolean[N];
    private static PriorityQueue<Node> heap = new PriorityQueue<>(N);
    private static int n, m, idx = 0;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String[] ss = reader.readLine().split(" ");
        n = Integer.parseInt(ss[0]);
        m = Integer.parseInt(ss[1]);
        Arrays.fill(h, -1);
        ss = reader.readLine().split(" ");
        for (int i = 0; i < n; i++) hw[i] = Integer.parseInt(ss[i]);
        for (int i = 0; i < m; i++) {
            ss = reader.readLine().split(" ");
            int a = Integer.parseInt(ss[0]), b = Integer.parseInt(ss[1]), c = Integer.parseInt(ss[2]);
            add(a, b, c);
            add(b, a, c);
        }
        int q = Integer.parseInt(reader.readLine());
        while (q-- > 0) {
            ss = reader.readLine().split(" ");
            int a = Integer.parseInt(ss[0]), b = Integer.parseInt(ss[1]), t = Integer.parseInt(ss[2]);
            int res = dijkstra(a, b, t);
            if (res == INF) writer.write("-1\n");
            else writer.write(res + "\n");
        }
        writer.flush();
    }

    private static int dijkstra(int start, int end, int t) {
        if (hw[start] > t || hw[end] > t) return INF;
        for (int i = 0; i < N; i++) {
            dist[i] = INF;
            st[i] = false;
        }
        heap.clear();
        heap.add(new Node(start, 0));
        dist[start] = 0;
        while (!heap.isEmpty()) {
            Node cur = heap.poll();
            int u = cur.u, d = cur.d;
            if (u == end) return d;
            if (st[u]) continue;
            st[u] = true;
            for (int i = h[u]; i != -1; i = ne[i]) {
                int v = e[i];
                if (hw[v] > t) continue;
                if (dist[v] > dist[u] + w[i]) {
                    dist[v] = dist[u] + w[i];
                    heap.add(new Node(v, dist[v]));
                }
            }
        }
        return INF;
    }

    private static void add(int a, int b, int c) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx;
        w[idx++] = c;
    }

    private static class Node implements Comparable<Node> {
        private int u, d;

        public Node(int u, int d) {
            this.u = u;
            this.d = d;
        }

        @Override
        public int compareTo(Node o) {
            return Integer.compare(d, o.d);
        }
    }
}
